package com.khadi.testcase.view.process;

import info.clearthought.layout.TableLayout;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;

import com.khadi.testcase.view.IWindowContent;

public class ProcessGraph implements IWindowContent, ActionListener {

	private static final String LINK_WORK_FLOW = "LINK_WORK_FLOW";
	private static final String MAP_WORK_FLOW = "MAP_WORK_FLOW";
	private boolean isWorkFlowSelected = true;

	@Override
	public Object getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Component getView() {
		double[] columns = new double[] { -1 };
		double[] rows = new double[] { -1 };

		TableLayout mainFetchLayout = new TableLayout(columns, rows);
		mainFetchLayout.setHGap(5);
		mainFetchLayout.setVGap(5);

		 

		JPanel panel = new JPanel(mainFetchLayout);
		panel.setBackground(Color.WHITE);

		// panel.addMouseListener(this);

		panel.add(new com.khadi.testcase.view.paint.PaintSurface(), "0,0");
		 

		 

		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(MAP_WORK_FLOW)) {
			isWorkFlowSelected = true;
		} else if (e.getActionCommand().equals(LINK_WORK_FLOW)) {
			isWorkFlowSelected = false;
		}

	}

	@Override
	public void postData(Object object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void preData(Object object) {
		// TODO Auto-generated method stub

	}

	private class PaintSurface1 extends JComponent implements MouseListener,
			MouseMotionListener {
		Point startDrag, endDrag;
		List<Shape> shapes = new ArrayList<Shape>();
		
		public PaintSurface1() {
			addMouseListener(this);
		}
		
		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			Graphics2D g2 = (Graphics2D) g;
			paintBackground(g2);
			
			
			g2.setStroke(new BasicStroke(2));
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
					0.50f));
			if (startDrag != null && endDrag != null) {
				Shape line = makeLine(startDrag.x, startDrag.y, endDrag.x,
						endDrag.y);
				g2.setPaint(Color.BLACK);
				g2.draw(line);
			}
		}

		private void paintBackground(Graphics2D g2) {
			g2.setPaint(Color.LIGHT_GRAY);
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			
			for (int i = 0; i < getSize().width; i += 12) {
				Shape line = new Line2D.Float(i, 0, i, getSize().height);
				g2.draw(line);
			}

			for (int i = 0; i < getSize().height; i += 12) {
				Shape line = new Line2D.Float(0, i, getSize().width, i);
				g2.draw(line);
			}
			
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			 
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent e) {
			startDrag = new Point(e.getX(), e.getY());
			endDrag = startDrag;
			repaint();

		}

		@Override
		public void mouseReleased(MouseEvent e) {
			endDrag = new Point(e.getX(), e.getY());
			repaint();

		}

		@Override
		public void mouseDragged(MouseEvent e) {
			endDrag = new Point(e.getX(), e.getY());
			repaint();

		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
			return new Line2D.Float(x1, y1, x2, y2);
		}
	}

	private class PaintSurface extends JComponent {
		ArrayList<Shape> shapes = new ArrayList<Shape>();

		Point startDrag, endDrag;

		public PaintSurface() {
			this.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					startDrag = new Point(e.getX(), e.getY());
					endDrag = startDrag;
					repaint();
				}

				public void mouseReleased(MouseEvent e) {

					if (isWorkFlowSelected) {
						shapes.add(makeRectangle(startDrag.x, startDrag.y, e
								.getX(), e.getY()));
					} else {
						shapes.add(makeLine(startDrag.x, startDrag.y, e.getX(),
								e.getY()));
					}
					startDrag = null;
					endDrag = null;
					repaint();
				}
			});

			this.addMouseMotionListener(new MouseMotionAdapter() {
				public void mouseDragged(MouseEvent e) {
					endDrag = new Point(e.getX(), e.getY());
					repaint();
				}
			});
		}

		private void paintBackground(Graphics2D g2) {
			g2.setPaint(Color.LIGHT_GRAY);
			for (int i = 0; i < getSize().width; i += 10) {
				Shape line = new Line2D.Float(i, 0, i, getSize().height);
				g2.draw(line);
			}

			for (int i = 0; i < getSize().height; i += 10) {
				Shape line = new Line2D.Float(0, i, getSize().width, i);
				g2.draw(line);
			}
		}

		public void paint(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			paintBackground(g2);
			Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN,
					Color.RED, Color.BLUE, Color.PINK };
			int colorIndex = 0;

			g2.setStroke(new BasicStroke(2));
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
					0.50f));

			for (Shape s : shapes) {
				g2.setPaint(Color.BLACK);
				g2.draw(s);
				g2.setPaint(colors[(colorIndex++) % 6]);
				g2.fill(s);
			}

			if (startDrag != null && endDrag != null) {
				g2.setPaint(Color.LIGHT_GRAY);

				if (isWorkFlowSelected) {
					g2.draw(makeRectangle(startDrag.x, startDrag.y, endDrag.x,
							endDrag.y));
				} else {
					g2.draw(makeLine(startDrag.x, startDrag.y, endDrag.x,
							endDrag.y));
				}
			}
		}

		private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
			return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2),
					Math.abs(x1 - x2), Math.abs(y1 - y2));
		}

		private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
			return new Line2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math
					.abs(x1 - x2), Math.abs(y1 - y2));
		}
	}
}
